<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="特约商户:" prop="userId">
        <el-select v-model="queryParams.userId" clearable placeholder="请选择特约商户">
          <el-option
            v-for="item in userDataList"
            :key="item.userId"
            :label="item.phonenumber + ' (' + item.userName + ')'"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付单号:" prop="withdrawalNo">
        <el-input
          style="width: 300px"
          v-model="queryParams.withdrawalNo"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter.native="handleQuery">
          <el-button circle slot="append" icon="el-icon-search" @click="handleQuery"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="提现状态:" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.withdrawal_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提现时间:">
        <el-date-picker
          v-model="queryParams.createTimeRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="到账时间:">
        <el-date-picker
          v-model="queryParams.reachTimeRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="agree"
        >同意申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="multiple"
          @click="reject"
        >驳回申请</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" row-key="id" @selection-change="handleSelectionChange">
      <el-table-column type="selection" :selectable="checkSelectable" width="50" align="center" />
      <el-table-column label="商户账号" prop="phonenumber"/>
      <el-table-column label="商户名称" prop="userName"/>
      <el-table-column label="支付单号" prop="withdrawalNo"/>
      <el-table-column label="金额" prop="amountTotal">
        <template slot-scope="scope">
          <span v-if="scope.row.amountTotal" style="color: red"> +{{ scope.row.amountTotal }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="提现手续费" prop="commission">
        <template slot-scope="scope">
          <span v-if="scope.row.commission">{{ scope.row.commission }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="实际到账" prop="realityAccountTotal">
        <template slot-scope="scope">
          <span v-if="scope.row.realityAccountTotal">{{ scope.row.realityAccountTotal }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="提现账户" width="300">
        <template slot-scope="scope">
          <div>{{ scope.row.bankAccount }}</div>
          <div>{{ scope.row.cardHolding }}</div>
          <div>{{ scope.row.bankName }}</div>
          <div>{{ scope.row.subbranch }}</div>
          <el-button
            type="primary"
            size="mini"
            @click="copyText(scope.row)"
          >复制</el-button>
        </template>
      </el-table-column>
      <el-table-column label="提取状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.withdrawal_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="提现时间" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到账时间" prop="reachTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reachTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失败原因" prop="failRemark"/>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope" v-if="scope.row.status === 1">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="done(scope.row)"-->
<!--          >打款成功-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="fail(scope.row)"-->
<!--          >打款失败-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            @click="agree(scope.row)"
          >同意申请
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="reject(scope.row)"
          >驳回申请
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { sysUserList,withdrawalDetailList,doneWithdrawal } from "@/api/triones/settledStore";
export default {
  name: "index",
  dicts: ['withdrawal_status'],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdrawalNo: undefined,
        userId: undefined,
        status: undefined,
        createTimeRange: [],
        reachTimeRange: []
      },
      userDataList: [],
      loading: true,
      total: 0,
      dataList: [],
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
    }
  },
  created() {
    this.getSysUserList();
    this.getList();
  },
  methods: {
    getSysUserList() {
      sysUserList().then(response => {
        this.userDataList = response.data;
      })
    },
    getList() {
      this.loading = true;
      withdrawalDetailList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 成功 */
    done(row) {
      const params = {
        id: row.id,
        status: 2,
        commission: 0
      }
      this.$prompt('请输入提现手续费(只能是整数或者小数,若没有手续费请填写0)', '打款成功', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'number',
        inputValue: 0,
      }).then(({ value }) => {
          let exp = /^[0-9]+([.][0-9]{1,})?$/;
          if(exp.test(value)){
            params.commission = value;
          } else {
            params.commission = 0;
          }
        doneWithdrawal(params).then(response => {
          this.$message("操作成功");
          this.getList();
        })
      }).catch(()=>{

      })
    },
    /** 失败 */
    fail(row) {
      const params = {
        id: row.id,
        status: 3,
        failRemark: ''
      }
      this.$prompt('请输入失败原因', '打款失败', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        params.failRemark = value;
        doneWithdrawal(params).then(response => {
          this.$message("操作成功");
          this.getList();
        })
      })
    },
    /** 同意 */
    agree(row) {
      const params = {
        ids: row.id || this.ids,
        status: 5,
        failRemark: ''
      }
      doneWithdrawal(params).then(response => {
        this.$message("操作成功");
        this.getList();
      })
    },
    /** 驳回 */
    reject(row) {
      const params = {
        ids: row.id || this.ids,
        status: 6,
        failRemark: ''
      }
      this.$prompt('请输入驳回原因', '驳回申请', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        params.failRemark = value;
        doneWithdrawal(params).then(response => {
          this.$message("操作成功");
          this.getList();
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/settledStore/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 复制文本 */
    copyText(row) {
      let copyText = row.bankAccount + '，' + row.cardHolding + '，' + row.bankName + '，' + row.subbranch + '，' + row.amountTotal + '元';
      const el = document.createElement('textarea');
      el.value = copyText;
      document.body.appendChild(el);
      el.select();
      document.execCommand('copy');
      document.body.removeChild(el);
      this.$message.success('复制成功');
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    checkSelectable(row) {
      return row.status === 1;
    },
  }
}
</script>

<style scoped>

</style>
